

#计算器

from tkinter import *
from tkinter import messagebox

def fallback():    
    len_entry = len(entry.get())
    entry.delete(len_entry - 1)

def clean():
    entry.delete(0,END)

def append_character(char):
    current_text = expression.get()
    if char == "=":
        try:
            result = eval(current_text)
            expression.set(str(result))
        except Exception as e:
            messagebox.showerror("错误", "无效的表达式")
    else:
        expression.set(current_text + char)

root=Tk()
root.title("计算器")
root.geometry('350x300')
root.resizable(0, 0)

menu = Menu(root)
fmenu = Menu(menu)
fmenu.add_command(label="退出")
menu.add_cascade(label="菜单",menu=fmenu)
root['menu'] = menu

#创建框架
frame = Frame(root,padx=100, pady=10)
frame.grid(row=1, column=0, columnspan=4, pady=10)

frame_compute=Frame(root)
frame_compute.grid(row=2, column=0, columnspan=4, pady=10)

#输入框
expression = StringVar()
entry = Entry(root,textvariable=expression)
entry.grid(row=0, column=0, columnspan=4)

#按钮
button_fallback=Button(frame,text="回退",command=fallback,font=('黑体', 10), width=10, height=2)
button_fallback.grid(row=0, column=1)

button_clean=Button(frame,text="清空",command=clean,font=('黑体', 10), width=10, height=2)
button_clean.grid(row=0, column=2)

#数值键
buttons = [
    '7', '8', '9', '/', 
    '4', '5', '6', '*', 
    '1', '2', '3', '-', 
    '0', '.', '=', '+'
]

row_val, col_val = 0, 0
for button in buttons:
    action = lambda x=button: append_character(x)
    Button(frame_compute, text=button, command=action, font=('黑体', 10), width=10, height=2).grid(row=row_val, column=col_val)
    col_val += 1
    if col_val > 3:
        col_val = 0
        row_val += 1


root.mainloop()







#图书管理系统


import tkinter as tk
from tkinter import messagebox
from tkinter import simpledialog
import hashlib
import random
import string

# 创建用户数据存储（模拟数据库）
users_db = {}

def generate_verification_code():
    """生成一个随机的验证码"""
    return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))

def hash_password(password):
    """对密码进行哈希处理"""
    return hashlib.sha256(password.encode()).hexdigest()

def login():
    """登录功能"""
    username = username_entry.get()
    password = password_entry.get()
    verification_code = verification_code_entry.get()
    
    if verification_code.lower() != verification_label.cget("text").lower():
        messagebox.showerror("错误", "验证码错误！")
        return
    
    if username in users_db and users_db[username] == hash_password(password):
        messagebox.showinfo("欢迎", f"欢迎 {username}！")
        root.destroy()  # 关闭登录窗口
    else:
        messagebox.showerror("错误", "用户名或密码错误！")

def register():
    """注册功能"""
    new_username = simpledialog.askstring("注册", "请输入用户名:")
    if not new_username or new_username in users_db:
        messagebox.showerror("错误", "用户名无效或已存在！")
        return
    
    new_password = simpledialog.askstring("注册", "请输入密码:", show='*')
    if not new_password:
        messagebox.showerror("错误", "密码不能为空！")
        return
    
    users_db[new_username] = hash_password(new_password)
    messagebox.showinfo("注册成功", "注册成功！")

# 创建主窗口
root = tk.Tk()
root.title("图书管理系统")
root.geometry('300x300')

# 创建和放置用户名标签和输入框
tk.Label(root, text="用户名:").grid(row=0, column=0, padx=10, pady=10)
username_entry = tk.Entry(root)
username_entry.grid(row=0, column=1, padx=10, pady=10)

# 创建和放置密码标签和输入框
tk.Label(root, text="密码:").grid(row=1, column=0, padx=10, pady=10)
password_entry = tk.Entry(root, show='*')
password_entry.grid(row=1, column=1, padx=10, pady=10)

# 生成和放置验证码标签和输入框
verification_code = generate_verification_code()
verification_label = tk.Label(root, text=verification_code)
verification_label.grid(row=2, column=0, columnspan=2, padx=10, pady=10)
verification_code_entry = tk.Entry(root)
verification_code_entry.grid(row=3, column=0, columnspan=2, padx=10, pady=10)

# 创建和放置登录按钮
login_button = tk.Button(root, text="登录", command=login)
login_button.grid(row=4, column=0, padx=10, pady=20)

# 创建和放置注册按钮
register_button = tk.Button(root, text="注册", command=register)
register_button.grid(row=4, column=1, padx=10, pady=20)

# 创建和放置退出按钮
exit_button = tk.Button(root, text="退出", command=root.quit)
exit_button.grid(row=5, column=0, columnspan=2, pady=10)

# 运行主循环
root.mainloop()






#简易记事本

from tkinter import *
from tkinter.scrolledtext import *
from tkinter import filedialog, messagebox, Menu

def new_file():
    text_area.delete(1.0, END)
    file_path = None

def open_file():
    file_path = filedialog.askopenfilename(defaultextension=".txt",filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
    if file_path:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
            text_area.delete(1.0, END)
            text_area.insert(END, content)
        file_path = file_path

def save_file(file_path):
    if file_path:
        with open(file_path, 'w', encoding='utf-8') as file:
            content = text_area.get(1.0, END)
            file.write(content)
    else:
        save_as_file()

def save_as_file():
    file_path = filedialog.asksaveasfilename(defaultextension=".txt",filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
    if file_path:
        with open(file_path, 'w', encoding='utf-8') as file:
            content = text_area.get(1.0, END)
            file.write(content)
        file_path = file_path

def exit_app():
    if messagebox.askokcancel("退出", "你确定要退出吗？"):
        root.quit()

def undo():
    try:
        text_area.edit_undo()
    except TclError:
        messagebox.showwarning("撤销", "没有可以撤销的操作")

def redo():
    try:
        text_area.edit_redo()
    except TclError:
        messagebox.showwarning("重做", "没有可以重做的操作")

def cut():
    try:
        text_area.event_generate("<<Cut>>")
    except TclError:
        messagebox.showwarning("剪切", "没有选中的文本可以剪切")

def copy():
    try:
        text_area.event_generate("<<Copy>>")
    except TclError:
        messagebox.showwarning("复制", "没有选中的文本可以复制")

def paste():
    try:
        text_area.event_generate("<<Paste>>")
    except TclError:
        messagebox.showwarning("粘贴", "剪贴板中没有可以粘贴的内容")

root = Tk()
root.title("简易记事本")
root.geometry("600x400")

text_area = ScrolledText(root,wrap=WORD,undo=True)
text_area.pack(expand=1, fill='both')

menu_bar = Menu(root)
root.config(menu=menu_bar)

file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="文件", menu=file_menu)
file_menu.add_command(label="新建", command=new_file)
file_menu.add_command(label="打开", command=open_file)
file_menu.add_command(label="保存", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="退出", command=exit_app)

edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="编辑", menu=edit_menu)
edit_menu.add_command(label="撤销", command=undo)
edit_menu.add_command(label="重做", command=redo)
edit_menu.add_separator()
edit_menu.add_command(label="剪切", command=cut)
edit_menu.add_command(label="复制", command=copy)
edit_menu.add_command(label="粘贴", command=paste)

root.mainloop()
















